﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using io = System.IO;

namespace editor.app
{
    public static class Extension
    {
        public static void SetLeft(this FrameworkElement el, double left)
        {
            el.SetValue(Canvas.LeftProperty, left);
        }
        public static double GetLeft(this FrameworkElement el)
        {
            return (double)el.GetValue(Canvas.LeftProperty);
        }
        public static void SetTop(this FrameworkElement el, double top)
        {
            el.SetValue(Canvas.TopProperty, top);
        }
        public static double GetTop(this FrameworkElement el)
        {
            return (double)el.GetValue(Canvas.TopProperty);
        }
        public static void SetLeftTop(this FrameworkElement el, double left, double top)
        {
            SetLeft(el, left);
            SetTop(el, top);
        }
        public static Point GetLeftTop(this FrameworkElement el)
        {
            return new Point(GetLeft(el), GetTop(el));
        }
        public static void SetZIndex(this FrameworkElement el, int zIndex)
        {
            el.SetValue(Canvas.ZIndexProperty, zIndex);
        }
        public static int GetZIndex(this FrameworkElement el, int zIndex)
        {
            return (int)el.GetValue(Canvas.ZIndexProperty);
        }
        public static void Drag(this FrameworkElement el, Action<FrameworkElement, MouseEventArgs, string, double, double> dragCallback)
        {
            var isDown = false;
            double offsetX = 0, offsetY = 0;
            el.MouseMove += new MouseEventHandler((o, e) =>
            {
                if (isDown)
                {
                    if (dragCallback != null)
                    {
                        dragCallback(el, e, "move", offsetX, offsetY);
                    }
                }
            });
            el.MouseLeftButtonUp += new MouseButtonEventHandler((o, e) =>
            {
                el.ReleaseMouseCapture();
                if (isDown && dragCallback != null)
                {
                    dragCallback(el, e, "up", offsetX, offsetY);
                }
                isDown = false;
            });
            el.MouseLeftButtonDown += new MouseButtonEventHandler((o, e) =>
            {
                var pt = e.GetPosition(el);
                offsetX = pt.X;
                offsetY = pt.Y;
                isDown = true;
                el.CaptureMouse();
                if (dragCallback != null)
                {
                    dragCallback(el, e, "down", offsetX, offsetY);
                }
            });
        }

        public static void FromBytes(this BitmapImage bitmapImage, byte[] bytes)
        {
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = new io.MemoryStream(bytes);
            bitmapImage.EndInit();
        }
        public static void FromFile(this BitmapImage bitmapImage, string fileName)
        {
            bitmapImage.FromBytes(io.File.ReadAllBytes(io.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName)));
        }

        public static void LoadConfig1(this Window window, bool center = true)
        {
            try
            {
                window.GetType().GetProperty("UseLayoutRounding").SetValue(window, true, null);
            }
            catch (Exception)
            {
            }
            window.SnapsToDevicePixels = true;
            window.FontFamily = new FontFamily("微软雅黑");
            window.FontSize = 13;

            if (center)
            {
                window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }
            window.KeyDown += new KeyEventHandler((o, e) =>
            {
                if (e.Key == Key.Escape)
                {
                    window.Close();
                }
            });

        }
    }

}
